package com.topvision.task;

import com.topvision.controller.AbstractController;
import com.topvision.model.ar.OffFace;
import com.topvision.service.ar.OffFaceService;
import com.topvision.service.glass.EquipmentService;
import org.apache.commons.io.FileUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

/**
 * @author shengwm
 * @create 2018-03-23 15:53
 **/
@Component("offFaceScheduler")
public class OffFaceScheduler extends AbstractController {


    @Autowired
    private OffFaceService offFaceService;

    @Autowired
    private EquipmentService equipmentService;


    @Value("#{APP_PROP['off.face.imglocation']}")
    private String imglocation;

    public void scheduleDel() throws IOException {
        logger.debug("定时执行   开始.....");
        LocalDate today = LocalDate.now();
        LocalDate yesterday = LocalDate.of(today.getYear(), today.getMonth(), today.getDayOfMonth() - 1);
        String yesterdate = yesterday.toString();
        String dateStr = today.toString();
        logger.debug(dateStr);
        List<OffFace> offFaces = offFaceService.findByDelTime(yesterdate);
        if (offFaces.size() > 0) {
            Integer integer = offFaceService.delbyschedule(yesterdate);
            if (integer > 0) {
                for (OffFace offface : offFaces) {
                    String[] codes = offface.getUrl().split("/");
                    String dir = imglocation + "/" + yesterdate + "/" + codes[5] + "/" + codes[6];
                    FileUtils.forceDelete(new File(dir));
                }


            }

        }


        logger.debug("定时执行  结束.....");
    }
}
